#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string>

pthread_cond_t Cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t Cond2 = PTHREAD_COND_INITIALIZER;
pthread_mutex_t Mutex = PTHREAD_MUTEX_INITIALIZER;

void *func1(void *args)
{
    while (true)
    {
        pthread_mutex_lock(&Mutex);
        std::cout << "我是线程A" << std::endl;
        pthread_cond_signal(&Cond2);
        pthread_cond_wait(&Cond1, &Mutex);
        pthread_mutex_unlock(&Mutex);
        // sleep(1);
    }
    return nullptr;
}

void *func2(void *args)
{
    while (true)
    {
        pthread_mutex_lock(&Mutex);
        std::cout << "我是线程B" << std::endl;
        pthread_cond_signal(&Cond1);
        pthread_cond_wait(&Cond2, &Mutex);
        pthread_mutex_unlock(&Mutex);
        // sleep(1);
    }
    return nullptr;
}

int main()
{

    pthread_t ta, tb;
    pthread_create(&ta, nullptr, func1, nullptr);
    pthread_create(&tb, nullptr, func2, nullptr);

    pthread_join(ta, nullptr);
    pthread_join(tb, nullptr);
    return 0;
}